package kz.kstu.scantest.desktop.report;

import kz.kstu.scantest.business.common.helper.FormDataHelper;
import kz.kstu.scantest.business.entity.data.FormData;
import kz.kstu.scantest.business.entity.data.dto.AnalyzeReportFormDataDTO;
import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;

import java.awt.*;
import java.io.InputStream;
import java.util.*;
import java.util.List;

/**
 * @author Filipp_Stankevich
 *         created: 19.09.11 21:16
 */
public class AnalyzeFormDataReport extends AbstractFormDataReport {

    private final static String REPORT_FILE = "reports/analyzeReport.jrxml";
    private final static String SUB_REPORT_FILE = "reports/analyzeAnswersReport.jrxml";

    @Override
    public JasperPrint buildReport(List<FormData> formDataList) throws JRException {
        ClassLoader classLoader = AbstractFormDataReport.class.getClassLoader();
        InputStream report = classLoader.getResourceAsStream(REPORT_FILE);
        JasperReport jasperReport = JasperCompileManager.compileReport(report);
        InputStream subReport = classLoader.getResourceAsStream(SUB_REPORT_FILE);
        JasperReport subJasperReport = JasperCompileManager.compileReport(subReport);
        List<AnalyzeReportFormDataDTO> dtoList = new ArrayList<AnalyzeReportFormDataDTO>();
        for (FormData formData : formDataList) {
            AnalyzeReportFormDataDTO dto = FormDataHelper.convertToAnalyzeDTO(formData);
            dtoList.add(dto);
        }
        Map<String, Object> parameters = new HashMap<String, Object>();
        parameters.put("subReport", subJasperReport);
        Image logoImage = getLogoImage();
        parameters.put("logo", logoImage);
        JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(dtoList);
        return JasperFillManager.fillReport(jasperReport, parameters, dataSource);
    }
}
